package br.com.portalecbahia.connectionservices;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Scanner;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

public class HTTPConnect {


	private String url;
	
	public HTTPConnect(String url) {
		this.url = url;
	}
	
	public JSONObject getObject(String method, ArrayList<NameValuePair> nameValuePairs) {
		DefaultHttpClient httpClient = new DefaultHttpClient();
    	
		HttpGet httpget = new HttpGet(url);

		HttpResponse response =null;
		try {
			response = httpClient.execute(httpget);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if(response!=null ){
	        HttpEntity entity = response.getEntity();
	        if (entity!=null) {

	        	if(response!=null && response.getStatusLine()!=null && response.getStatusLine().getStatusCode()==200){
					try {

			            String res = new Scanner(response.getEntity().getContent(), "UTF-8").useDelimiter("\\A").next();

			            
			            JSONObject json = new JSONObject(res);
			            return json;

					} catch (IllegalStateException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					} catch (JSONException e) {
						e.printStackTrace();
					}


	        	}
	        }
		}
		return null;
	}
	
	

}

